import { Layers, Node, SpriteFrame, UITransform } from "cc";

export const createUINode = (name: string = '') => {
    const node = new Node(name)
    const transform = node.addComponent(UITransform)
    transform.setAnchorPoint(0, 1)
    node.layer = 1 << Layers.nameToLayer("UI_2D")
    return node;
}

/***
 * 生成指定长度随机uuid
 * @param n
 */
export const randomByLength = (n: number) =>
  Array.from({ length: n }).reduce<string>((total: string) => total + Math.floor(Math.random() * 10), '')

  
export const randomByRange = (start: number, end: number) => {
    return Math.floor(start + (end - start) * Math.random())
}

const reg = /\((\d+)\)/

const getNumberWithStr = (str: string) => parseInt(str.match(reg)[1] || '0')

export const sortSpriteFrame = (spriteFrames: SpriteFrame[]) =>
    spriteFrames.sort((a, b) => getNumberWithStr(a.name) - getNumberWithStr(b.name))